package com.nefu.softlab.xjwc.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import static com.nefu.softlab.xjwc.common.Constant.APPLICATION_NAME;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author LiXiwen
 * @date 2020/1/7 20:27
 */
@Component
public final class LoggerUtil {
    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public void info(String msg) {
        send(new Exception().getStackTrace()[1].getClassName() + " : " + new Exception().getStackTrace()[1].getMethodName(), "info", msg);
    }

    public void warn(String msg) {
        send(new Exception().getStackTrace()[1].getClassName() + " : " + new Exception().getStackTrace()[1].getMethodName(),"warn", msg);
    }

    public void error(String msg) {
        send(new Exception().getStackTrace()[1].getClassName() + " : " + new Exception().getStackTrace()[1].getMethodName(),"error", msg);
    }

    public void debug(String msg) {
        send(new Exception().getStackTrace()[1].getClassName() + " : " + new Exception().getStackTrace()[1].getMethodName(),"debug", msg);
    }

    public void send(String className, String level, String content) {
        synchronized(LoggerUtil.class) {
            LogVo logVo = new LogVo();
            logVo.setLevel(level);
            logVo.setApplication(APPLICATION_NAME);
            logVo.setContent(content);
            logVo.setTag(className);
            logVo.setTimestamp(LocalDateTime.now().format(dtf));

            String baseUrl = "http://118.24.41.50:8081";
            WebClient webClient = WebClient.create(baseUrl);
            Mono<Object> mono = webClient.post().uri("/log/send").bodyValue(logVo).retrieve().bodyToMono(Object.class);
            Object res = mono.block();
        }
    }
}
